﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;

namespace Demo.Common
{
    /// <summary>
    /// GetPageData 的摘要说明
    /// </summary>
    public class GetPageData : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            int CurrentPage = string.IsNullOrEmpty(context.Request["page"]) ?
                1 : Convert.ToInt32(context.Request["page"]);
            int ItemsPerPage = string.IsNullOrEmpty(context.Request["limit"]) ?
                10 : Convert.ToInt32(context.Request["limit"]);

            //ItemsPerPage = 20;

            string typeId = context.Request["TypeId"];


            try
            {
                PetaPoco.Sql sql = PetaPoco.Sql.Builder.Append("select 物料ID,物料名称,物料编码,物料分类ID from 基础_物料");
                if (!string.IsNullOrEmpty(typeId))
                {
                    sql.Where("物料分类ID=@0", typeId);
                }
                sql.OrderBy("物料编码");

                Demo.Data.MatrixData data = new Data.MatrixData("datuhe");
                Demo.Data.MatrixDataPage pageData = data.FetchPageDataTable(CurrentPage, ItemsPerPage, sql);

                ExtJSExtender.Controls.GridDataStore dataStore = new ExtJSExtender.Controls.GridDataStore();
                dataStore.CurrentPage = (int)pageData.CurrentPage;
                dataStore.TotalCount = (int)pageData.TotalItems;

                //var converter = new ExtJSExtender.DataRowJSConverter();
                //foreach (DataRow row in pageData.Data.Rows)
                //{
                //    DataTable dt = new DataTable();
                //    DataRowCollection rows = dt.Rows;
                //    List<DataRow> rowList = new List<DataRow>();
                    
    
                //    dataStore.Data.Add((Dictionary<string, object>)(converter.Serialize(row, ExtJSExtender.Util.Serializer)));
                //}
                //dataStore.Data = (Dictionary<string, object>)(new ExtJSExtender.DataRowJSConverter().Serialize(pageData.Data.Rows, ExtJSExtender.Util.Serializer)); //new List<Dictionary<string, object>>();
                dataStore.BindData(pageData.Data);
                context.Response.ContentType = "text/json";
                context.Response.Write(ExtJSExtender.Util.SerializeToJson(dataStore));
            }
            catch
            {
                ExtJSExtender.Controls.GridDataStore dataStore = new ExtJSExtender.Controls.GridDataStore();
                dataStore.CurrentPage = CurrentPage;
                dataStore.TotalCount = 1024;
                dataStore.Data = new List<Dictionary<string, object>>();
                var dic = new Dictionary<string, object>();
                dic.Add("物料ID", "1");
                dic.Add("物料名称", "桌子");
                dic.Add("物料编码", "0001");
                dic.Add("物料分类ID", "00");
                dataStore.Data.Add(dic);
                dic = new Dictionary<string, object>();
                dic.Add("物料ID", "2");
                dic.Add("物料名称", "椅子");
                dic.Add("物料编码", "0002");
                dic.Add("物料分类ID", "00");
                dataStore.Data.Add(dic);
                dic = new Dictionary<string, object>();
                dic.Add("物料ID", "3");
                dic.Add("物料名称", "拖把");
                dic.Add("物料编码", "0003");
                dic.Add("物料分类ID", "00");
                dataStore.Data.Add(dic);

                context.Response.ContentType = "text/json";
                context.Response.Write(ExtJSExtender.Util.SerializeToJson(dataStore));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}